#include "bullet2.h"
#include "enemy2.h"
#include "window3.h"
#include <QPainter>
#include <QPropertyAnimation>

bullet2::bullet2(QPoint start_pos,QPoint end_pos,int damage,Enemy2 *Target,window4 *game2,QString filename) : QObject(0)
{
    this->start_pos=start_pos;
    this->end_pos=end_pos;
    this->current_pos=start_pos;
    _damage=damage;
    QPixmap pic(filename);
    picture=pic;
    Target_enemy=Target;
    this->game2=game2;
    pic_size=QSize(picture.width(),picture.height());
}
void bullet2::draw(QPainter *painter){
    painter->drawPixmap(current_pos,picture);
}
void bullet2::move(){
    static const int duration=50;
    QPropertyAnimation *animintion=new QPropertyAnimation(this,"current_pos");
    animintion->setDuration(duration);
    animintion->setStartValue(start_pos-QPoint(pic_size.width()/2,pic_size.height()/2));
    animintion->setEndValue(end_pos);
    connect(animintion,SIGNAL(finished()),this,SLOT(hit_enemy()));
    animintion->start();
}

QPoint bullet2::getcurrent_pos(){
    return current_pos;
}

void bullet2::setcurrent_pos(QPoint pos){
    current_pos=pos;
}

void bullet2::hit_enemy(){
  if(game2->enemy_list().indexOf(Target_enemy)!=-1)
      Target_enemy->get_damage(_damage);
  game2->remove_bulluet(this);
}
